package artid.com.arttogo.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.CollecterActivity;
import artid.com.arttogo.activity.OrganizationActivity;
import artid.com.arttogo.bean.ArtList;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.task.CallbackSky;

public class ShootAdapter extends BaseAdapter {
	private Context mContext;
	LayoutInflater mInflater;
	ViewHolder holder;
	CallbackSky cs;
	public List<ArtList> listdata;
	private ImageLoader mImageLoader;
	private ImageLoader mImageLoader2;
	public ShootAdapter(Context mContext) {
		this.mContext = mContext;
		mInflater = LayoutInflater.from(mContext);
		RequestQueue mQueue = Volley.newRequestQueue(mContext);
		mImageLoader = new ImageLoader(mQueue, new BitmapCache());
		mImageLoader2 = new ImageLoader(mQueue, new BitmapCache());
		listdata = new ArrayList<ArtList>();
	}

	@Override
	public int getCount() {
		return listdata.size();
	}


	public void adddata(List<ArtList> listdata) {
		this.listdata.addAll(listdata);
		notifyDataSetChanged();
	}
	public void setdata(List<ArtList> listdata) {
		this.listdata = listdata;
		notifyDataSetChanged();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_shoot, null);
			vh.title = (TextView) convertView.findViewById(R.id.item_artlist_title);
			vh.author = (TextView) convertView.findViewById(R.id.item_artlist_name);
			vh.iv = (ImageView) convertView.findViewById(R.id.item_artlist_maining);
			vh.icon = (ImageView) convertView.findViewById(R.id.item_artlist_icon);
			vh.iconClick = (View)convertView.findViewById(R.id.search_click);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		final ArtList item = listdata.get(position);
		if (item.getTitle()==null) {
			item.setTitle("");
		}
		if (item.getRealname()==null) {
			item.setRealname("");
		}
		vh.title.setText(item.getTitle());
		if (!item.getRealname().equals("null")) {
			vh.author.setText(StringUtils.substring(item.getRealname(),0,4));
		}else {
			vh.author.setText("");
		}
		
//		vh.iv.setTag(item.getPic_url()+AppUtil.picWidthUrl);
//		ImageListener listener = VolleyBasic.getInstance((Activity)mContext).
//				getImageListener(vh.iv, R.drawable.ic_menu_rotate, R.drawable.ic_menu_rotate,item.getPic_url()+AppUtil.picWidthUrl);
//		mImageLoader.get(item.getPic_url()+AppUtil.picWidthUrl, listener);
//		
		Glide.with(mContext).load(item.getPic_url()+ AppUtil.picWidthUrl)
		.placeholder(R.drawable.ic_menu_rotate)
		.error(R.drawable.ic_menu_rotate).into(vh.iv);
		if (item.getPath().contains("http")) {
			vh.icon.setTag(item.getPath());
			ImageListener listener2 = VolleyBasic.getInstance((Activity)mContext).
					getImageListener(vh.icon, R.drawable.ic_menu_rotate, R.drawable.ic_menu_rotate,item.getPath());
			mImageLoader.get(item.getPath(), listener2);
		}
		vh.iconClick.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (item.getIs_type()==2) {
					Intent intent = new Intent(mContext,OrganizationActivity.class);
					intent.putExtra("name", item.getRealname());
					intent.putExtra("id", item.getArtist_id());
					mContext.startActivity(intent);
				}else {
					Intent intent = new Intent(mContext,CollecterActivity.class);
					intent.putExtra("id", item.getArtist_id());
					mContext.startActivity(intent);
				}
			}
		});
		

		return convertView;
	}

	class ViewHolder {
		TextView title, author;
		ImageView icon; ImageView iv;
		View iconClick;

	}
}
